/** @file    InterfacesProvider.cpp
 *  @author  Alessandro Polo
 *  @version $Id: InterfacesProvider.cpp 2833 2010-08-02 23:12:57Z alex $
 *  @brief
 * File containing methods for the wosh::InterfacesProvider class.
 * The header for this class can be found in InterfacesProvider.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <core/InterfacesProvider.h>

 #include <core/ObjectFactory.h>
 #include <core/Functional.h>
 #include <core/DataModels.h>
 #include <core/List.h>
 #include <core/Table.h>


using namespace std;
namespace wosh {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 WOSH_REGISTER(wosh::InterfacesProvider, "wosh::ISerializable", 1.01, _static_InterfacesProvider)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

InterfacesProvider::~InterfacesProvider() {
	clear();
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT InterfacesProvider::add( ObjectTypeInfo* interface_info ) {
	if ( interface_info == NULL ) return WRET_ERR_PARAM;
	MutexLockerWrite mL(getMutex());
	ConstIterator it;
	const ConstIterator it_end = end();
	for ( it=begin(); it!=it_end; ++it ) {
		if ( it->second == NULL ) continue;
		if ( it->second == interface_info ) return WRET_ERR_PARAM;
		if ( *it->second == *interface_info ) return WRET_ERR_PARAM;
	 }
	set(interface_info->getName(), interface_info);
	return WRET_OK;
 }

WRESULT InterfacesProvider::add( const std::string& classType, double interfaceVersion ) {
	ObjectTypeInfo* bint = new ObjectTypeInfo();
	bint->setName( classType );
	bint->setVersion( interfaceVersion );
	WRESULT ret = add(bint);
	if ( WFAILED(ret) )
		delete bint;
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
WRESULT InterfacesProvider::createFor( const IReflection* object ) {
	if ( object == NULL ) return WRET_ERR_PARAM;
	return createFor(object->getClassName());
 }

WRESULT InterfacesProvider::createFor( const string& classname ) {
	std::vector<ObjectTypeInfo> parents;
	WRESULT ret = ObjectFactory::gatherInfoOf(parents, classname, 0.0 );
	if ( WFAILED(ret) )
		return ret;
	transactionBeginWrite();
	free();
	std::vector<ObjectTypeInfo>::const_iterator it;
	for ( it=parents.begin(); it!=parents.end(); ++it ) {
		ObjectTypeInfo* ii = new ObjectTypeInfo(*it);
		if ( MapT<std::string, ObjectTypeInfo*>::exists( (*it).getName() ) ) continue;
		set(ii->getName(), ii);
	 }
	transactionEnd();
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool InterfacesProvider::existsClass( const string& classname, double min_version ) const {
	MutexLockerRead mL(getMutex());
	if ( min_version == 0.0 )
		return MapT<std::string, ObjectTypeInfo*>::exists(classname);
	const ObjectTypeInfo* info = find(classname);
	if ( info->getVersion() >= min_version )
		return true;
	return false;
 }

ObjectTypeInfo InterfacesProvider::getImplementedClass() const {
	ConstIteratorReverse it = this->rbegin();
	if ( it == this->rend() ) return ObjectTypeInfo();
	ObjectTypeInfo info;
	info.setName(it->first);
	if ( it->second != NULL )
		info.setVersion(it->second->getVersion());
	return info;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

InterfacesProvider& InterfacesProvider::operator=(const InterfacesProvider& m) {
	if (this == &m) return *this; // same object?
	m.copyTo(*this);
	return *this;
 }

bool InterfacesProvider::operator==(const InterfacesProvider &other) const {
	if ( this->count() != other.count() )
		return false;
	return true;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT InterfacesProvider::fromString( const std::string& data ) {
	vector<string> tokens;
	Utilities::splitString(data, ";", tokens, false, true);
	vector<string>::iterator it;
	double cVer = 0.0;
	for ( it=tokens.begin(); it!=tokens.end(); ++it ) {
		if ( (*it).find(",") == string::npos )
			cVer = 0.0;
		else {
			;// BUG
		 }
		add( *it, cVer );
	 }
	return WRET_OK;
 }

std::string InterfacesProvider::toString() const {
	string temp = "";
	ConstIterator it;
	const ConstIterator it_end =end();
	for ( it=begin(); it!=it_end; ++it ) {
		if ( it->second == NULL ) continue;
		if ( it->second->getVersion() == 0.0 )
			temp += it->second->getName() + ";";
		else
			temp += Utilities::format("%s,%g;", it->second->getName().c_str(), it->second->getVersion());
	 }
	return temp;
 }

bool InterfacesProvider::updateHash( DataHashContext* hash_context ) const {
	if ( hash_context == NULL ) return false;
	bool ret = true;
	ConstIterator it;
	const ConstIterator it_end =end();
	for ( it=begin(); it!=it_end; ++it ) {
		ret = hash_context->update( it->first ) & ret;
		if ( it->second == NULL ) continue;
		ret = hash_context->update( it->second->getName() ) & ret;
		ret = hash_context->update( it->second->getVersion() ) & ret;
	 }
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

List* InterfacesProvider::toList() const
 {
	transactionBeginRead();
	List* objs = new List(size());
	ConstIterator it;
	const ConstIterator it_end = end();
	for ( it=begin(); it!=it_end; ++it ) {
		if ( it->second == NULL ) continue;
		objs->push_back<Variant>( it->second->getName() );
	 }
	transactionEnd();
	return objs;
 }

Table* InterfacesProvider::toTable() const
 {
	transactionBeginRead();
	Table* table = new Table( size(), 2, true );
	table->setTableName("interfaces");
	table->setHeaderHorizontal("Type", 0);
	table->setHeaderHorizontal("Version", 1);
	unsigned int iRow = 0;
	ConstIterator it;
	const ConstIterator it_end = end();
	for ( it=begin(); it!=it_end; ++it, ++iRow ) {
		if ( it->second== NULL ) continue;
		table->set<Variant>( it->second->getName(), iRow, 0 );
		table->set<Variant>( it->second->getVersion(), iRow, 1 );
	 }
	transactionEnd();
	return table;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

}; // namespace wosh
